package com.itheima.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

//UDP接收数据：因为接收端不知道发送端什么时候停止发送，故采用死循环接收
public class ReceiveDemo02 {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(10086);
        while (true){
            //接收数据的数组
            byte[] bytes = new byte[1024];
            DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
            //接收数据
            ds.receive(dp);
            //打印
            String s = new String(bytes, 0, dp.getLength());
            System.out.println(s);

            //发现接收的是886，就退出
            if ("886".equals(s)){
                break;
            }
        }
        ds.close();

    }
}
